﻿//===================================================================
//
// Description: Sparqube Picture Column Lite for SharePoint 2010
// Web: http://www.sparqube.com/
//
//-------------------------------------------------------------------
//
// Copyright (C) 2012 ArtfulBits.CZ (development[at]artfulbits.cz)
// Web: http://www.artfulbits.cz/
//
//-------------------------------------------------------------------
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A 
// PARTICULAR PURPOSE.
//===================================================================

using System;
using System.Web.UI;

using Microsoft.SharePoint;

namespace Sparqube.SharePoint.PictureColumnLite
{
	public class PictureLiteValue :
		SPFieldUrlValue
	{
		#region Fields

		private string m_cssClass = "";

		#endregion //Fields

		#region Constructors

		public PictureLiteValue()
		{
			base.Url = "";
			base.Description = "";
		}

		public PictureLiteValue(string fieldValue)
		{
			SimpleHtmlFieldValue htmlValue = new SimpleHtmlFieldValue(fieldValue, HtmlTextWriterTag.Img);
			base.Url = htmlValue[HtmlTextWriterAttribute.Src];
			base.Description = "";
			m_cssClass = htmlValue[HtmlTextWriterAttribute.Class];
		}

		#endregion //Constructors

		#region Properties

		public new string Url
		{
			get
			{
				return base.Url;
			}
			set
			{
				string description = this.Description;
				base.Url = value;
				base.Description = description;
			}
		}

		public string CssClass
		{
			get
			{
				return m_cssClass;
			}
			set
			{
				m_cssClass = value;
			}
		}

		#endregion //Properties

		#region Overrides

		public override string ToString()
		{
			string result = "";
			if (!string.IsNullOrEmpty(this.Url))
			{
				SimpleHtmlFieldValue htmlValue = new SimpleHtmlFieldValue(HtmlTextWriterTag.Img);
				htmlValue[HtmlTextWriterAttribute.Src] = this.Url;
				htmlValue[HtmlTextWriterAttribute.Class] = this.CssClass;
				htmlValue[HtmlTextWriterAttribute.Style] = "border-width: 0px;";
				result = htmlValue.ToString();
			}
			return result;
		}

		#endregion //Overrides

		#region Implementation

		internal static PictureLiteValue Parse(object value)
		{
			PictureLiteValue urlValue = null;
			if (value != null)
			{
				if (value is string)
				{
					string strVal = (string)value;
					if (!string.IsNullOrEmpty(strVal))
					{
						urlValue = new PictureLiteValue((string)value);
					}
				}
				else
				{
					if (!(value is PictureLiteValue))
					{
						throw new ArgumentException("value");
					}
					urlValue = (PictureLiteValue)value;
				}
			}
			return urlValue;
		}
		#endregion //Implementation

	}// PictureLiteValue
}
